/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.dao.hibernate;

import br.com.fatecpg.songs.dao.util.HibernateSession;
import br.com.fatecpg.songs.entity.util.Searchable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;

/**
 *
 * @author Marcos
 */
public abstract class SearchableDAO<E extends Searchable> extends GenericDAO<E> implements br.com.fatecpg.songs.dao.SearchableDAO<E>{
    
    public SearchableDAO(Class<? extends Searchable> clazz) {
        super(clazz);
    }
    //método para realizar pesquisa parametrizada
    @Override
    public List<E> search(String searchedWord){                
        Session session = HibernateSession.currentSession();
        Criteria c = session.createCriteria(clazz);
        setAlias(c);
        setOrder(c);
        c.add(getSearchCriterion(searchedWord));
        c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return c.list();        
    }        
    
    //método para definir os alias e seus joins
    public abstract void setAlias(Criteria c);
    
    //método para definir por qual(is) campo(s) será ordenada a consulta
    public abstract void setOrder(Criteria c);    
    
    //método para definir as restrições de pesquisa
    @Override
    public abstract Criterion getSearchCriterion(String searchedWord);
}
